﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OGS.Contracts;
using OGS.Utility;
using OGS.Web.Models.Admin;

namespace OGS.Web.Controllers.Admin
{
    public partial class AdminController : AdminBaseController
    {
        /// <summary>
        /// Save Category Action Result
        /// </summary>
        /// <returns></returns>
        [ActionName("Save-Category")]
        public ActionResult SaveCategory(int? ID)
        {
            SaveCategoryModel SaveCategoryModel = new SaveCategoryModel();
            SaveCategoryModel.IsActive = true;
            if (ID.HasValue)
            {
                //Get Category from DB
                SaveCategoryModel.GetCategoryDetails(ID.Value);

                SaveCategoryModel.IsSuccess = Session["Success"] == null ? false : true;
                Session["Success"] = null;

            }
            SaveCategoryModel.PopulateParentCategories();
            SaveCategoryModel.ParentCategories.Insert(0, new Category { ID = 0, ComputedName = "--- Select ---" });
            return View("~/Views/Admin/Category/SaveCategory.aspx", SaveCategoryModel);
        }

        /// <summary>
        /// Save Category in DB
        /// </summary>
        /// <param name="oSaveCategoryModel"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ActionName("Save-Category")]
        public ActionResult SaveCategory(SaveCategoryModel SaveCategoryModel)
        {
            if (ModelState.IsValid)
            {
                if (SaveCategoryModel.CategoryImage != null)
                {
                    string ImageName = DateTime.Now.Ticks.ToString() + ".jpg";
                    SaveCategoryModel.CategoryImage.SaveAs(Server.MapPath("/UploadedFiles/CategoryImages/") + ImageName);
                    SaveCategoryModel.Image = ImageName;
                }

                Result result = (Result)SaveCategoryModel.SaveCategory();
                if (result == Result.Success)
                {
                    Session["Success"] = true;
                    return Redirect("/admin/save-Category/" + SaveCategoryModel.ID.ToString());
                }
                else if (result == Result.Duplicate)
                {
                    ModelState.AddModelError("EnglishName", "The name already exists.");
                }
                else if (result == Result.DuplicateCode)
                {
                    ModelState.AddModelError("Code", "The code already exists.");
                }
                else
                {
                    ModelState.AddModelError("EnglishName", "Some error occurred. Please try again later.");
                }

            }
            SaveCategoryModel.PopulateParentCategories();
            SaveCategoryModel.ParentCategories.Insert(0, new Category { ID = 0, ComputedName = "--- Select ---" });
            return View("~/Views/Admin/Category/SaveCategory.aspx", SaveCategoryModel);
        }

        /// <summary>
        /// Category List Action
        /// </summary>
        /// <returns></returns>
        [ActionName("Category-List")]
        public ActionResult CategoryList()
        {
            SearchAttributes SearchAttributes = GetSearchAttributesFromQuesryString();

            string Name = Helper.GetQueryStringValue("Name");
            int ParentCategoryID = -1;
            if (Request.QueryString["ParentCategory"] != null)
            {
                ParentCategoryID = Helper.GetQueryStringValueInt("ParentCategory");
            }

            CategoryListModel CategoryListModel = new CategoryListModel();
            CategoryListModel.Populate(Name, ParentCategoryID, SearchAttributes);
            CategoryListModel.SearchAttributes = SearchAttributes;

            return View("~/Views/Admin/Category/CategoryList.aspx", CategoryListModel);
        }
    }
}
